<?php 

namespace Home\Controller;
use \Think\Controller;

//分类控制类
class ClassifyController extends Controller
{
    public function index()
    {
        $id = $_GET['id'];
        // session('classify.id',$id);
        $id = I('get.id/d');
        $data = M('category')->where("id='$id'")->find();
        if ($data == null) {$this->error('无效操作');}
        if($pname = M('category')->where('id='.$data['pid'])->find()){
            $ppname = M('category')->where('id='.$pname['pid'])->find();
            if (empty($ppname)) {
                $ppname = null;
            }
        }
        if (empty($pname)){
            $pname = null;
        }
        $data2 = M('category')->where('pid = '.$id)->select();
        $this->assign('data2',$data2);
        $this->assign('pid',$pname['id']);
        $this->assign('pname',$pname['name']);
        $this->assign('ppname',$ppname['name']);
        $this->assign('classify',$data['name']);
        $this->assign('title','分类');
        $data3 = array();
        foreach ($data2 as $key => $val) {
            $content= M('experience')->order('id desc')->where('category_id = '.$val['id'])->select();
            $content2= M('category')->where('pid = '.$val['id'])->select();
            $data3[] = $content;
                foreach ($content2 as $key => $value) {
                    $content2 = M('experience')->order('id desc')->where('category_id = '.$value['id'])->select();
                    $data3[] = $content2;
                }
        }
        $data4 = array();
        foreach ($data3 as $key => $val2) {
            foreach ($val2 as $key => $val3) {
                $data4[] = $val3;
            }
        }
        $data5 = M('experience')->order('id desc')->where('category_id = '.$id)->select();
        foreach ($data5 as $key => $value) {
            $data4[] = $value;
        }
        $this->assign('content',$data4);

        //分享达人
        $people_data = M('experience')->field('uid,count(uid)')->order('count(uid) desc')->limit(3)->group('uid')->select();
        $user_info = array();
        foreach ($people_data as $key => $val) {
            $model = M('user')->field('name')->where('id = '.$val['uid'])->find();
            $model_path = M('user_info')->field('thumbpath,fans,signature,uid')->where('uid = '.$val['uid'])->find();
            // print_r($model);
            $info = array_merge($model,$model_path);
            $info2['user_info'] = $info;
            $info2['user_info'][count] = $val['count(uid)'];
            // $user_info[] = $info;
            $model = M('experience')->field('id,title')->limit(4)->where('uid = '.$val['uid'])->select();
            $model2['experience'] = $model;
            $info = array_merge($model2,$info2);
            
            $user_info[] = $info;
        }

        $this->assign('show_man',$user_info);

        $this->display('Classify/index2');
    }

}